import { createStore } from 'vuex'
import axios from 'axios'
// 创建仓库实例
const store = createStore({
  // 严格模式
  strict: true,
  // 存储 公共状态
  state: {
    count: 1,
    cates: []
  },
  getters: {
    doubleCount (state) {
      return state.count * 2
    },
    cateLen (state) {
      return state.cates.length
    }
  },
  // mutations 存储修改 state 方法  api.it120.cc
  mutations: {
    ADD_NUM (state, num) {
      state.count += num
    },
    INIT_CATES (state, cates) {
      state.cates = cates
    }
  },
  // actions 定义异步请求
  actions: {
    /*
      action 第一个参数 就是 store实例对象
    */
    FETCH_CATES ({ commit }, params = {}) {
      axios.get('https://api.it120.cc/conner/cms/category/list', { params }).then(res => {
        if (res.data.code === 0) {
          commit('INIT_CATES', res.data.data)
        }
      })
    }
  }
})

export default store
